let that;
import {
	getMyUserHomeInfo,
	getUserInfo
} from "@/static/js/my.js";
import {
	getMessage_count
} from "@/static/js/news.js"
import {
	appJumpWx
} from '@/utils/utils.js'
import {
	getShopList
} from '@/static/js/mall.js'
import {
	completeTask
} from '@/static/js/points.js';

export default {
	data() {
		return {
			userInfo: {},
			usernameTxt: '',
			menuList1: [{
					icon: '/static/img/my/index/icon-wxgzh.png',
					name: '关注公众号',
					remark: '',
					url: '',
					handler: "toAppJumpWx"
				},
				// {
				// 	icon: '/static/img/my/index/icon-sc.png',
				// 	name: '我的收藏',
				// 	remark: '',
				// 	url: '/pages/my/myCollection/index'
				// },
				// {
				// 	icon: '/static/img/my/index/icon-xz.png',
				// 	name: '我的下载',
				// 	remark: '',
				// 	url: '/pages/my/myHome/myDownload/myDownload'
				// },
				// {
				// 	icon: '/static/img/my/index/icon-dd.png',
				// 	name: '我的订单',
				// 	remark: '',
				// 	url: '/pages/my/myHome/myOrders/myOrders',
				// 	handler: ''
				// },
				// {
				// 	icon: '/static/img/my/index/icon-zyrs.png',
				// 	name: '专业人员',
				// 	remark: '',
				// 	url: '/pages/my/major/index',
				// 	handler: 'getMajors'
				// },
				// {
				// 	icon: '/static/img/my/index/icon-yjfk.png',
				// 	name: '意见反馈',
				// 	remark: '',
				// 	url: '/pages/my/feedback/index',
				// },
				{
					icon: '/static/img/my/index/icon-lxwm.png',
					name: '联系我们',
					remark: '',
					url: '',
					handler: 'WXOpenCustomerServiceChat'
				},

				{
					icon: '/static/img/my/index/icon-sz.png',
					name: '设置',
					remark: '',
					url: '/pages/my/settings/index',
				}
			],

			modalShow: false,
			//认证专业人士
			majorTypes: '',
			systemPopupShow: false,
			showPcDscPopup: false,
			hintList: [{
				name: '我的提问',
				num: 0,
				type: 1,
				url: '/quiz/myAsk/index',
			}, {
				name: '我的卡券',
				num: 0,
				type: 1,
				url: '/pages/my/coupon/index',
			}, {
				name: '消息通知',
				num: 0,
				type: 2,
				url: '/pages/message/index/index',
			}], //提示列表

			giftList: [], //礼物列表
			isLogin: false, //是否登录

			showPointsTips: false, //任务完成提示
			pointsObj: {}, //任务完成对象
		}
	},
	onReady() {
		uni.setNavigationBarColor({
			frontColor: '#ffffff',
			backgroundColor: '#000000'
		})
	},
	onLoad(options) {
		this.getShopList()
	},
	onShow(e) {
		that = this;
		if (uni.getStorageSync("token")) {
			this.userInfo.mobile = '1'
			this.getUserInformation()
			this.getMyUserHomeInfo()
			this.isLogin = true
		} else {
			this.userInfo = {}
			console.log(this.userInfo, 'userInfo');
			this.majorTypes = ''
			this.isLogin = false
			this.hintList[0].num = 0
			this.hintList[1].num = 0
			this.hintList[2].num = 0
		}

		this.systemPopupShow = this.$store.state.inform

		let msgNum = (this.$store.state.serverNum + this.$store.state.wdNum)
		this.hintList[2].num = msgNum > 99 ? '99+' : '' + msgNum
	},
	methods: {
		goMyHome() {
			if (!uni.getStorageSync("token")) {
				uni.navigateTo({
					url: '/pages/login/login'
				})
				return
			}
			if (this.userInfo.is_major == 1) {
				uni.navigateTo({
					url: '/pages/my/homeCenter/teacherHome'
				})
			} else {
				uni.navigateTo({
					url: '/pages/my/homeCenter/home'
				})
			}
		},

		//打开微信客服
		WXOpenCustomerServiceChat() {

			this.completeQuest('B7')
			let wechat = null
			plus.share.getServices(res => {
				wechat = res.find(i => i.id === 'weixin')
				if (wechat) {
					wechat.openCustomerServiceChat({
						corpid: 'ww447880e03d7eba27', //企业ID
						url: 'https://work.weixin.qq.com/kfid/kfc8c70c43a4de355ad', //客服地址
					}, src => {
						console.log("success:", JSON.stringify(src))
					}, err => {
						console.log("error:", JSON.stringify(err))
					})
				} else {
					uni.showToast({
						title: '当前环境不支持微信操作',
						icon: "error"
					});
				}
			}, function() {
				uni.showToast({
					title: "获取服务失败，不支持该操作。" + JSON.stringify(e),
					icon: 'none'
				})
			});
			// #ifndef APP-PLUS

			// #endif
		},

		async getMessages() {
			const res = await getMessage_count({})
			if (res.code == 1) {
				let num = Number(res.data.comment) + Number(res.data.like) + Number(res.data.message) + Number(
					res.data.question) + Number(res.data.wendahuifu)
				this.$store.commit('set_wdNum', num);
				let serverNum = this.$store.state.serverNum ? this.$store.state.serverNum : 0
				if ((num + serverNum) > 0) {
					console.log('我的num', num, serverNum);
					uni.setTabBarBadge({
						index: 3,
						text: (num + serverNum) > 99 ? '99+' : '' + (num + serverNum)
					})
				} else {
					uni.removeTabBarBadge({
						index: 3
					})
				}
			}
		},

		//去购买会员页面
		toPlatform() {
			if (!uni.getStorageSync("token")) {
				this.getUserInformation()
				return
			}
			let platform = uni.getSystemInfoSync().platform
			uni.navigateTo({
				url: '/pages/my/vip/index'
			})
		},

		// 去充值
		goZhuyeView() {
			if (!uni.getStorageSync("token")) {
				this.$tools.noTokenGo()
				return
			}
			let platform = uni.getSystemInfoSync().platform
			if (platform == 'android') {
				if (this.userInfo.mobile) {
					uni.navigateTo({
						url: '/pages/my/bamboo/index'
					})
				}

			}
		},
		// 获取用户信息
		async getUserInformation() {
			getUserInfo().then(res => {
				this.userInfo = res.data;
				this.hintList[0].num = res.data.quiznum
				this.hintList[1].num = res.data.jianghu_coupon
			})
		},

		getMajors() {
			let major_status = this.userInfo.major_status
			this.$tools.href('/pages/my/major/index');
		},
		//点击菜单跳转
		clickMenu(item) {
			if (!uni.getStorageSync("token")) {
				this.getUserInformation()
				return
			}
			if (item.handler) this[item.handler]()
			else if (item.url) this.$tools.href(item.url)
		},
		async getMyUserHomeInfo() {
			let {
				code,
				data,
				msg
			} = await getMyUserHomeInfo()
			if (code == 1) {
				this.majorTypes = data.major_types
			}
		},

		//跳转到微信公众号
		toAppJumpWx() {
			appJumpWx('gh_54c7ed1b5bfc')
			// uni.removeStorageSync('installTime')
			// uni.removeStorageSync('openTime')
			// console.log(this.$store.state.inform);
			// console.log('清除installTime');
		},

		//跳转到签到
		signIn() {
			if (!uni.getStorageSync("token")) {
				uni.navigateTo({
					url: '/pages/login/login'
				})
				return
			}
			this.$tools.href('/pointsMall/index/index')
		},

		//显示PC端介绍
		showPcDsc() {
			this.showPcDscPopup = true
		},

		//拷贝链接
		copyLink() {
			uni.setClipboardData({
				data: 'www.xzcs2022.com',
				success: function() {
					uni.showToast({
						title: '复制成功',
						icon: 'none'
					});
				}
			});
		},

		pcDscJump() {
			this.showPcDscPopup = false
			uni.navigateTo({
				url: '/pages/pcDsc/index'
			})
		},

		// 点击提示栏
		hintClick(e) {
			if (e.type == 1) {
				uni.navigateTo({
					url: e.url
				})
			} else {
				uni.switchTab({
					url: e.url
				})
			}
		},


		//企服和我的邀请跳转
		onService(e) {
			let token = uni.getStorageSync("token")
			if (!token) {
				uni.navigateTo({
					url: '/pages/login/login'
				})
				return
			}
			if (e == 1) {
				uni.showToast({
					title: '敬请期待',
					icon: 'none'
				})
				// uni.navigateTo({
				// 	url: '/pages/my/invite/myInvite'
				// })
			} else {
				uni.navigateTo({
					url: '/enterpriseServer/serverCentre/index'
				})
			}
		},

		// 请求商品列表
		async getShopList() {
			let {
				code,
				data,
				msg
			} = await getShopList()
			if (code == 1) {
				this.giftList = data
			} else {
				this.$tools.msg(msg)
			}
		},

		//去商城
		goPointsMall(url) {
			if (!uni.getStorageSync("token")) {
				this.$tools.noTokenGo()
				return
			}
			uni.navigateTo({
				url: url
			})
		},
		//商品详情
		goShopDetail(val) {
			uni.navigateTo({
				url: '/pointsMall/shopDetail/index?id=' + val.id
			})
		},
		//去兑换
		goExchangeAll() {
			uni.navigateTo({
				url: '/pointsMall/shopList/index'
			})
		},

		//完成任务
		completeQuest(code) {
			completeTask({
				code
			}).then(res => {
				if (res.code == 1) {
					this.showPointsTips = true;
					this.pointsObj = res.data;
				}
			})
			setTimeout(() => {
				this.showPointsTips = false;
			}, 3000);
		},
	}
}